package sample;



import javafx.embed.swing.SwingFXUtils;
import javafx.scene.canvas.Canvas;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MaskImage extends JFrame{




    public   BufferedImage maskImage(String maskURL,String sourceURL,int width,int height)throws Exception {

        BufferedImage rImage = null;
        if(BeautyMain.themeURL==""||BeautyMain.themeURL==null){
            return null;
        }
        BufferedImage maskImg = ImageIO.read(new File(maskURL));
        BufferedImage sourceImg = ImageIO.read(new File(sourceURL));
        try {
            int maskW = maskImg.getWidth();
            int maskH = maskImg.getHeight();
            int[] mPixs = new int[maskW * maskH];//蒙版像素
            int sourceW = sourceImg.getWidth();
            int sourceH = sourceImg.getHeight();
            int[] sPixs = new int[sourceW * sourceH];//原图片像素

            PixelGrabber maskPG = new PixelGrabber(maskImg, 0, 0, maskW, maskH, mPixs, 0, maskW);
            maskPG.grabPixels();
            PixelGrabber sourcePG = new PixelGrabber(sourceImg, 0, 0, sourceW, sourceH, sPixs, 0, sourceW);
            sourcePG.grabPixels();

            int[] rPixels = new int[maskW * maskH];//结果图片像素
            for (int i = 0; i < rPixels.length; i++) {
                if (mPixs[i] == -16777216) {
                    //背景透明处理
                    rPixels[i] = sPixs[i];
                }
            }

            Image resultImage = createImage(new MemoryImageSource(sourceW, sourceH, rPixels, 0, sourceW));
            rImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics g = rImage.getGraphics();
            g.drawImage(resultImage, 0, 0, null);

            //return g;

        } catch (Exception e) {

        }
        //WritableImage writableImage = SwingFXUtils.toFXImage(rImage, new WritableImage(rImage.getWidth(), rImage.getHeight()));
       return  rImage;
    }

}
